package com.pz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pz.gulimall.product.entity.SkuImagesEntity;
import com.pz.gulimall.product.entity.resultMap.SkuInfoResultMap1;
import com.pz.gulimall.product.vo.res.ProductInfoResVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.product.entity.SkuInfoEntity;
import com.pz.gulimall.product.service.SkuInfoService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.validation.constraints.NotEmpty;

/**
 * sku信息
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-01 20:22:27
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * sku检索 包含参数 --- key: 检索关键字；catelogId: 分类检索
     *                    brandId: 品牌检索
     *                    min: 0, max: 0 价格区间检索
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPageCondition(params);
        if((page.getCurrPage()-1)*page.getPageSize() > page.getTotalCount()){
            page.setCurrPage(1);
        }
        return R.ok().put("page", page);
    }

    @PostMapping("/list/getSpuBrandSku")
    public R getSpuBrandSku(@RequestBody @NotEmpty List<Long> skuIds){
        List<SkuInfoResultMap1> res = skuInfoService.queryBatchBySkuIds(skuIds);
        return R.ok().put("data", res);
    }

    @PostMapping("/list/byIds")
    public R getByIds(@RequestBody @NotEmpty List<Long> skuIds){
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        skuIds.forEach(skuId -> {
            queryWrapper.or().eq("sku_id", skuId);
        });
        List<SkuInfoEntity> skuInfoEntities = skuInfoService.list(queryWrapper);
        return R.ok().put("data", skuInfoEntities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfoEntity){
		save(skuInfoEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

    @RequestMapping("/search_info/{skuId}")
    public R searchInfo(@PathVariable Long skuId) throws ExecutionException, InterruptedException {
        ProductInfoResVo ans = skuInfoService.queryProductInfo(skuId);
        return R.ok().put("data", ans);
    }

}
